package router

import (
	"iris-wms/apps/admin/web/dto/din"
	"iris-wms/apps/admin/web/service"
	"iris-wms/common/tools"
	"iris-wms/common/tools/result"
	"path"
	"path/filepath"
	"strconv"

	"github.com/kataras/iris/v12"
	"github.com/yitter/idgenerator-go/idgen"
)

func NewWmsGoodsRoute() *wmsGoodsRoute {
	return &wmsGoodsRoute{}
}

type wmsGoodsRoute struct{}

func (r *wmsGoodsRoute) Create(ctx iris.Context, service_ service.WmsGoodsService) {
	var in din.WmsGoodsIn
	if err := ctx.ReadJSON(&in); err != nil {
		ctx.StopWithPlainError(iris.StatusBadRequest, err)
		return
	}
	// https://github.com/hotoo/pinyin
	// in.Goods.PinYin = pinyin.Pinyin(in.Goods.Name, pinyin.NewArgs())
	in.Goods.Uid = tools.GetUserID(ctx)
	if err := service_.Create(in); err != nil {
		ctx.StopWithPlainError(iris.StatusInternalServerError, err)
		return
	}
	// if _, err = tools.GBboltDB.Incr(); err != nil {
	// 	goto ERR
	// }
	result.OK(ctx)
}

func (r *wmsGoodsRoute) Edit(ctx iris.Context, service_ service.WmsGoodsService) {
	var in din.WmsGoodsIn
	if err := ctx.ReadJSON(&in); err != nil {
		ctx.StopWithPlainError(iris.StatusBadRequest, err)
		return
	}
	if err := service_.Update(in); err != nil {
		ctx.StopWithPlainError(iris.StatusBadRequest, err)
		return
	}
	// if _, err = tools.GBboltDB.Incr(); err != nil {
	// 	goto ERR
	// }
	result.OK(ctx)
}

func (r *wmsGoodsRoute) UpImage(ctx iris.Context, service_ service.WmsGoodsService) {
	var pre string = tools.GConfig.App.File.Image
	f, fh, err := ctx.FormFile("file")
	defer f.Close()
	if err != nil {
		ctx.StopWithPlainError(iris.StatusInternalServerError, err)
		return
	}

	var filename = strconv.FormatInt(idgen.NextId(), 10) + path.Ext(fh.Filename)
	_, err = ctx.SaveFormFile(fh, filepath.Join(pre, "/skill/", filename))
	if err != nil {
		ctx.StopWithPlainError(iris.StatusInternalServerError, err)
		return
	}

	//url = "http://localhost:8084/" + fh.Filename
	//support.Ok(ctx, define.CODE_OK, url)
	result.OKData(ctx, iris.Map{
		"name": filename,
		"path": "/skill/" + filename,
		"ext":  path.Ext(fh.Filename),
	})
}

func (r *wmsGoodsRoute) Delete(ctx iris.Context, service_ service.WmsGoodsService) {
	var idList = make([]uint, 0)
	if err := ctx.ReadJSON(&idList); err != nil {
		ctx.StopWithPlainError(iris.StatusBadRequest, err)
		return
	}
	if err := service_.DeleteByIdList(idList); err != nil {
		ctx.StopWithPlainError(iris.StatusInternalServerError, err)
		return
	}
	result.OK(ctx)
}

func (r *wmsGoodsRoute) Table(ctx iris.Context, service_ service.WmsGoodsService) {
	var in din.WmsGoodsTableIn
	if err := ctx.ReadQuery(&in); err != nil {
		ctx.StopWithPlainError(iris.StatusBadRequest, err)
		return
	}
	rows, total, err := service_.GetTable(in)
	if err != nil {
		ctx.StopWithPlainError(iris.StatusInternalServerError, err)
		return
	}
	result.VxeTableData(ctx, in.PageSize, in.CurrentPage, total, rows, nil)
}

func (r *wmsGoodsRoute) List(ctx iris.Context, service_ service.WmsGoodsService) {
	rows, err := service_.GetList()
	if err != nil {
		ctx.StopWithPlainError(iris.StatusInternalServerError, err)
		return
	}
	result.OKData(ctx, rows)
}

func (r *wmsGoodsRoute) GetGoodsByCode(ctx iris.Context, service_ service.WmsGoodsService) {
	var word = ctx.URLParamDefault("word", "")
	one, err := service_.GetByCode(word)
	if err != nil {
		ctx.StopWithPlainError(iris.StatusInternalServerError, err)
		return
	}
	result.OKData(ctx, one)
}
